package com.nuoyuan.wx.admin.controller;

import com.nuoyuan.wx.common.controller.BaseController;
import com.nuoyuan.wx.vo.LoginVo;
import com.nuoyuan.wx.vo.TokenVo;
import com.nuoyuan.wx.aop.NoLoginRequired;
import com.nuoyuan.wx.common.config.AdminConfig;
import com.nuoyuan.wx.common.utils.GsonUtil;
import com.nuoyuan.wx.service.respcode.AdminRespCodeEnum;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Package com.nuoyuan.wx.admin.controller
 * @ClassName SystemController
 * @Description TODO
 * @Author linus
 * @Date 2020-02-13 14:19
 * @Version 1.0
 **/
@RestController
public class SystemController extends BaseController {

    /**
     * 登陆
     * @param param
     * @param request
     * @param response
     * @return
     */
    @NoLoginRequired
    @RequestMapping(value = "/admin/login", method = RequestMethod.POST)
    @ResponseBody
    public String login(@RequestBody LoginVo param, HttpServletRequest request, HttpServletResponse response) {
        if(null == param ||
                !AdminConfig.username.equalsIgnoreCase(param.getUsername()) ||
                !AdminConfig.pwd.equalsIgnoreCase(param.getPassword())) {
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            return this.buildErrorResp(AdminRespCodeEnum.LOGIN_PARAM_ERROR.getMsg());
        }
        String token = this.buildToken(AdminConfig.username, AdminConfig.pwd);
        return GsonUtil.toJson(new TokenVo(token));
    }
}
